package com.example.user.customviewdemo.test;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.widget.EditText;

import com.example.user.customviewdemo.R;

/**
 * Created by user on 2016/5/11.
 */
public class DeleteEditTextView extends EditText {
    private Bitmap bm;
    private Paint paint;
    private Rect rect;
    public DeleteEditTextView(Context context) {
        super(context);
    }

    public DeleteEditTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
        paint = new Paint(Paint.ANTI_ALIAS_FLAG);
        bm = BitmapFactory.decodeResource(getResources(), R.mipmap.brick);
        Bitmap b = Bitmap.createBitmap(150,100, Bitmap.Config.ARGB_4444);
    }

    public DeleteEditTextView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        String text = getText().toString();
        if(text == null || "".equals(text)){
            return;
        }
        int left = getWidth() - 150;
        int top = getHeight()-130;
        int right = getWidth();
        int bottom = getHeight() - 30;

        rect = new Rect(left,top,right,bottom);
        canvas.drawBitmap(bm,null,rect,paint);

    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        float x = event.getX();
        float y = event.getY();

        if (rect != null && rect.contains((int)x,(int)y)){
            setText("");
        }
        return super.onTouchEvent(event);
    }
}
